import 'package:flutter/material.dart';
import 'package:flutter_application_cyber/model/cn_net_response.dart';
import 'package:flutter_application_cyber/net/cn_net_helper.dart';
import 'package:flutter_application_cyber/pages/Mian/cn_login_page.dart';
import 'package:flutter_application_cyber/model/cn_user_default_cache.dart';
import 'package:flutter_application_cyber/pages/Mian/cn_tabaBar_page.dart';
import '../../model/cn_strategy_model.dart';
import 'package:provider/provider.dart';
import 'package:flutter_application_cyber/model/cn_strategy_vmodel.dart';
import 'package:flutter_application_cyber/tools/cn_log_pv_manger.dart';

class CnWelcomePage extends StatefulWidget {
  const CnWelcomePage({Key? key}) : super(key: key);

  @override
  State<CnWelcomePage> createState() => _CnWelcomePageState();
}

class _CnWelcomePageState extends State<CnWelcomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
      decoration: BoxDecoration(
        image: DecorationImage(
            image: AssetImage('images/icon_wecome_bg.png'), fit: BoxFit.fill),
      ),
      child: Column(
        children: [
          Container(
            margin: EdgeInsets.only(top: 340),
            width: double.infinity,
            child: Column(
              children: [
                Container(
                  width: 88,
                  height: 88,
                  child: Image.asset(
                    'images/app_logo.png',
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 10),
                  child: Text(
                    'CyberNex',
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 20,
                        fontWeight: FontWeight.bold),
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    ));
  }

  @override
  void initState() {
    super.initState();

//启动埋点
    CnLogPvManger.stat(
        page: 'launch',
        subPage: 'subPage',
        terPage: 'terPage',
        broadcasterId: 'broadcasterId');

    CnUserDefaultCacheHelper.getAuthToken().then((value) async {
      if (value.isNotEmpty) {
        //校验token
        CnNetHelper.postIsValidToken(value,
            (HDHResponseData? configData, bool result, String msg) async {
          bool isLogin = configData?.data ?? false;
          bool getConfigSuccess = await CnNetHelper.getAppConfig();
          await Provider.of<CnStrategyViewModel>(context, listen: false)
              .questStrategyBlock();
          if (isLogin) {
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => const CnTabaBarPage()));
          } else {
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => const CnLoginPage()));
          }
        });
      } else {
        //请
        Navigator.push(context,
            MaterialPageRoute(builder: (context) => const CnLoginPage()));
      }
    });
  }
}
